import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class AMFServer extends JFrame
{
	// instance
	public static AMFServer serviceserver;
	private ServiceSocket serviesocket;
	
	
	// Swing
	private JScrollPane pane_output;
	private JTextArea area_output;
	private JPanel panel_buttons;
	private JButton btn_exit;
	private JButton btn_start;


	
	private String policy;
	private boolean started;
	
	public static void main(String[] args) {
		AMFServer ss = new AMFServer();
		//policyserver.run();
		ss.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				System.exit(0);
			}
		});
	}
	
	AMFServer()
	{
		super("AMFServer");
		serviceserver = this;
		started = false;
		
		createAndShowGUI();
		//makePolicy();
		makeSocket();
	}
	
	public static AMFServer getInstance()
	{
		return serviceserver;
	}
	
	private void makeSocket()
	{
		serviesocket = new ServiceSocket();
	}
	
	/*
	private void makePolicy()
	{
		policy = "";
		
		File f;
		FileReader in = null;
		try {
			f = new File("./", "crossdomain.xml");
			in = new FileReader(f);
			char[] buffer = new char[4096];
			int len;
			while((len=in.read(buffer))!=-1){
				String s = new String(buffer, 0, len);
				policy = s + policy;
			}
		} 
		catch (IOException e) {
			System.out.println("IOException on socket listen: " + e);
		} 
		finally {
			try{ if (in!=null) in.close(); } catch (IOException e) {}
		}
	}
	*/
	
	private void createAndShowGUI()
	{
		// 판넬 생성
		area_output = new JTextArea(13, 45);
		area_output.setEditable(false);
		pane_output = new JScrollPane(area_output,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

		// 버튼 생성
		btn_start = new JButton("Start");
		btn_exit = new JButton("Exit");
		
		btn_start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(started){
					started = false;
					serviesocket.stopServer();
					btn_start.setLabel("Start");
				} else {
					started = true;
					serviesocket = new ServiceSocket();
					serviesocket.startServer();
					btn_start.setLabel("Stop");
				}
			}
		});
		
		btn_exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		// 버튼 등록
		panel_buttons = new JPanel();
		panel_buttons.setLayout(new FlowLayout());
		panel_buttons.add(btn_start);
		panel_buttons.add(btn_exit);

		// 판넬 등록
		Container container = this.getContentPane();
		container.add(pane_output, BorderLayout.CENTER);
		container.add(panel_buttons, BorderLayout.SOUTH);

		// this.setLayout(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(540, 320);
		this.pack();
		this.setVisible(true);
	}
	
	public void out(String $message) {
		System.out.println($message);
		area_output.append($message+ "\n\r");
		int pos = area_output.getText().length();
		area_output.setCaretPosition(pos-1);
		area_output.requestFocus();
	}

	/**
	 * @param policy the policy to set
	 */
	public void setPolicy(String policy)
	{
		this.policy = policy;
	}

	/**
	 * @return the policy
	 */
	public String getPolicy()
	{
		return policy;
	}
}